local timetool = {}

function timetool:getCurrentDay()
    local currentTimeStamp = ngx.now()
    local targetTimeStamp = self:strtotimestamp(os.date("%Y-%m-%d 6:00:00", ngx.now()))
    ngx.log(ngx.ERR,currentTimeStamp,targetTimeStamp)
    if currentTimeStamp > targetTimeStamp then
        return os.date("%Y-%m-%d",ngx.now())
    else
        return self:daystep(os.date("%Y-%m-%d",ngx.now()),-1)
    end
end



function timetool:daystep(old_day,step)
    local y,m,d
    if("0" ~= string.sub(old_day,6,6)) then
        m=string.sub(old_day,6,7)
    else
        m=string.sub(old_day,7,7)
    end

    if("0" ~= string.sub(old_day,9,9)) then
        d=string.sub(old_day,9,10)
    else
        d=string.sub(old_day,10,10)
    end

    y=string.sub(old_day,0,4)
   
    local old_time=os.time{year=y,month=m,day=d}
    local new_time=old_time+86400*step

    local new_day=os.date("*t",new_time)
    local res=""

    if(tonumber(new_day.day)<10 and tonumber(new_day.month)<10)then
        res=new_day.year.."-".."0"..new_day.month.."-".."0"..new_day.day
    elseif tonumber(new_day.month)<10 then
        res=new_day.year.."-".."0"..new_day.month.."-"..new_day.day
   
    elseif tonumber(new_day.day)<10 then
        res=new_day.year.."-"..new_day.month.."-".."0"..new_day.day
    else
        res=new_day.year.."-"..new_day.month.."-"..new_day.day
    end
    return res
end


--[[
--获取时间戳
----]]
function timetool:strtotimestamp(timeToConvert)

    local ymdhis = "(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)"

    local runyear,runmonth,runday = self:getymd(timeToConvert);
    local runhour,runminute,runseconds = self:gethis(timeToConvert);
    local convertedTimestamp = os.time({
                                           year = runyear,
                                           month = runmonth,
                                           day = runday,
                                           hour = runhour,
                                           min = runminute,
                                           sec = runseconds
                                       });
    return convertedTimestamp;
end

--[[
获取年、月、日
--]]
function timetool:getymd(str)
    local ymd =  "(%d+)-(%d+)-(%d+)"
    local md =  "(%d+)-(%d+)"
    local runyear, runmonth, runday = str:match(ymd);

    if runyear then
        return runyear, runmonth, runday;
    else
        local runmonth, runday = str:match(md);
        if runmonth then
             return nil,runmonth, runday;
        else
            return nil,nil,nil;
        end
    end

end

--[[
获取时、分、秒
--]]
function timetool:gethis(str)
    local his = "(%d+):(%d+):(%d+)"
    local hi = "(%d+):(%d+)"

    local runhour, runminute, runseconds = str:match(his);
    if runhour then
        return runhour, runminute, runseconds;
    else
        local runhour, runminute = str:match(hi);
        if runhour then
             return runhour, runminute, nil;
        else
            return nil,nil, nil;
        end
    end

end

return timetool
